#!/bin/bash
:<<!
脚本用来初始化deepin的基础环境，包括：jdk，maven，zookeeper，node，IDE,webstorm
1.脚本需要使用root权限执行,尽量使用root身份执行，而不是sudoer。
2.脚本需要会将原始文件存放在/data/download/init目录下面。
2.所有的文件都会安装在/usr/local目录下。
!
#基础常量
downloadDir=/data/download/init
#
# 安装jdk
#
function installJava(){ 
echo "
 ************************************************************
 ****    will install the java development environment   ****
 ************************************************************
"
echo "step1:download Java SE 8u181 ..."
#下载jdk
wget  -nc --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.tar.gz
if [ $? -gt 0 ]
  then 
    echo "the jdk file download fail...";
    exit 2
fi
#解压jdk
tar -zxf jdk-8u181-linux-x64.tar.gz
mv jdk1.8.0_181 /usr/local/java
#配置环境变量
java_env_exist=$(awk '{print}' /etc/profile |grep "#JAVA_SETTINGS@JPANDA" &>>/dev/null;echo $?)
if [ $java_env_exist -gt 0 ]
  then 
    echo "
#JAVA_SETTINGS@JPANDA
export JAVA_HOME=/usr/local/java
export JRE_HOME=\${JAVA_HOME}/jre 
export CLASSPATH=.:\${JAVA_HOME}/lib:\${JRE_HOME}/lib 
export PATH=\${JAVA_HOME}/bin:\$PATH
#JAVA
">>/etc/profile
fi
source /etc/profile
# 修改sudoers执行脚本目录
# sed -i 's/\(^Defaults\s\+secure_path\s\+=\s\+.*\)/\1:/usr/local/java/bin/g' /etc/sudoers
ln -s /usr/local/java/bin /usr/local/bin/java
echo "java development environment is complated... "
java -version
}
#
# 安装node
#
function installNode(){
echo "
 ************************************************************
 ****    will install the node development environment   ****
 ************************************************************
"
wget -nc https://nodejs.org/dist/v10.6.0/node-v10.6.0-linux-x64.tar.xz
if [ $? -gt 0 ]
  then 
    echo "the node file download fail...";
    exit 2
fi
#安装基础环境
apt-get install -y g++ ibssl-dev
#
tar -xvf node-v10.6.0-linux-x64.tar.xz
mv node-v10.6.0-linux-x64 /usr/local/node
ln -s /usr/local/node/bin/node /usr/local/bin/node
ln -s /usr/local/node/bin/npm /usr/local/bin/npm
#安装nrm
npm install -g nrm
}
#
# 安装maven
#
function installMaven(){
echo "
 ************************************************************
 ****             will install the maven                 ****
 ************************************************************
"
wget -nc http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
if [ $? -gt 0 ]
  then 
    echo "the maven file download fail...";
    exit 2
fi
tar -zxvf apache-maven-3.5.4-bin.tar.gz
mv apache-maven-3.5.4 /usr/local/maven
#配置环境变量
maven_env_exist=$(awk '{print}' /etc/profile |grep "#MAVEN_SETTINGS@JPANDA" &>>/dev/null;echo $?)
if [ $maven_env_exist -gt 0 ]
  then 
    echo "
#MAVEN_SETTINGS@JPANDA
export M2_HOME=/usr/local/maven
export PATH=\$M2_HOME/bin:\$PATH
#MAVEN
">>/etc/profile
fi
ln -s /usr/local/maven/bin /usr/local/bin/maven
source /etc/profile
}
#
#安装Zookeeper
#
function installZookeeper(){
echo "
 ************************************************************
 ****             will install the zookeeper             ****
 ************************************************************
"
wget -nc http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
tar -zxvf zookeeper-3.4.13.tar.gz
mv  zookeeper-3.4.13 /usr/local/zookeeper
#配置环境变量
maven_env_exist=$(awk '{print}' /etc/profile |grep "#ZOOKEEPER_SETTINGS@JPANDA" &>>/dev/null;echo $?)
if [ $maven_env_exist -gt 0 ]
  then 
    echo "
#ZOOKEEPER_SETTINGS@JPANDA
export ZOOKEEPER_HOME=/usr/local/zookeeper/
export PATH=\$ZOOKEEPER_HOME/bin:\$PATH
export PATH
#ZOOKEEPER END
">>/etc/profile
fi
}
#
#安装subversion
#
function installSubversion(){
apt-get install -y subversion
}
#
#安装git
#
function isntallGit(){
apt-get install -y git
}

#
#安装atom
#
#function installAtom(){
#wget -nc https://atom.io/download/deb
#dpkg -i atom-amd64.deb
#}

#
#安装IDEA
#
function installIDEA(){
echo "
 ************************************************************
 ****             will install the IDEA             ****
 ************************************************************
"
wget -nc https://download.jetbrains.com/idea/ideaIU-2018.1.6.tar.gz
tar -zxvf ideaIU-2018.1.6.tar.gz
mv idea-IU-181.5540.7 /usr/local/idea
nohup /usr/local/idea/bin/idea.sh &
}

#
#安装Webstorm
#
function installWebstorm(){
echo "
 ************************************************************
 ****             will install the webstorm             ****
 ************************************************************
"
wget -nc https://download.jetbrains.com/webstorm/WebStorm-2018.1.5.tar.gz
tar -zxvf WebStorm-2018.1.5.tar.gz
mv WebStorm-181.5281.31 /usr/local/webstorm
nohup /usr/local/webstorm/bin/webstorm.sh &
}
#************************************************************
#  			     START
#************************************************************
#校验是否以root身份运行
echo "Script permission check..."
if [ `id -u` -gt 0 ];
  then 
    echo 'please use  the root user to execute this script';
    exit 2;
fi

# 校验/data/download/init
echo "directory permission check..."
dirExist=$(ls /data/download/init &>/dev/null; echo $?);
if [ $dirExist -gt 0 ]
  then 
    echo "create /data/download/init...";
    mkdir -p /data/download/init;
    chmod -R 4755 /data
fi

# 进入脚本执行目录
cd $downloadDir

# 备份profile文件
echo "Backup profile ... "
cp /etc/profile /data/download/init/profile.backup
source /etc/profile
# 安装JDK
java_exist=$(java -version &>/dev/null;echo $?)
if [ $java_exist -gt 0 ]
  then 
    echo "install jdk...";
    installJava;
else
  echo "jdk already installed...skip..."
fi

# 安装NODE和NPM
node_exist=$(node -v &>/dev/null;echo $?)
if [ $node_exist -gt 0 ]
  then 
    echo "install node and npm...skip...";
    installNode;
else
  echo "node and npm already installed...skip..."
fi

# 安装maven
maven_exist=$(mvn -v &>/dev/null;echo $?)
if [ $maven_exist -gt 0 ]
  then 
    echo "installmaven...";
    installMaven;
else
  echo "maven alrady installed...skip..."
fi

# 安装zookeeper
zookeeper_exist=$(zkEnv.sh &>/dev/null;echo $?)
if [ $zookeeper_exist -gt 0 ]
  then 
    echo "install zookeeper...";
    installZookeeper;
else
  echo "zookeeper already installed...skip..."
fi

#安装subversion
subversion_exist=$(svn help &>/dev/null;echo $?)
if [ $subversion_exist -gt 0 ]
  then 
    echo "install subversion...";
    installSubversion;
else
  echo "subversion already installed...skip..."
fi

#安装git
git_exist=$(git help &>/dev/null;echo $?)
if [ $git_exist -gt 0 ]
  then 
    echo "install git...";
    isntallGit;
else
  echo "git already installed...skip..."
fi
# 安装IDEA
idea_exist=$(ls /usr/local/bin/idea &>/dev/null;echo $?)
if [ $idea_exist -gt 0 ]
  then 
    echo "install IDEA...";
    installIDEA;
else
  echo "IDEA already installed...skip..."
fi

# 安装Webstorm
webstorm_exist=$(ls /usr/local/bin/webstorm &>/dev/null;echo $?)
if [ $webstorm_exist -gt 0 ]
  then 
    echo "install Webstorm...";
    installWebstorm;
else
  echo "Webstorm already installed...skip..."
fi

echo "complated..."
